﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Services;
using System.Web.UI.HtmlControls;
using Com.ThrongIn.BLL;
using Com.ThrongIn.BE;
using Com.ThrongIn.Utilities;
namespace ThrongIn
{
    public partial class Profiles : BasePage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                UserControl ctrl =  this.Master.FindControl("BreadCrumb") as UserControl ;
                if (ctrl != null) ctrl.Visible = true;
                hidProfileId.Value  = Convert.ToString(Request.QueryString["cid"]);
                int pageCount;
                List<Profile> profiles = GetProfiles(Utility.ItemPerPage,0, Convert.ToInt32(hidProfileId.Value), out pageCount);
                if (profiles != null && profiles.Count == 0)
                {
                    divMsg.Visible = true;
                    divMsg.InnerHtml = "No data available at this moment for selected category.";
                    divMsg.Attributes.Add("class", "error");                    
                }
                ucProfilePerson.DataSource = profiles;
                hidPage.Value = pageCount.ToString();
                ucProfilePerson.BindControl();
            }
        }
        [WebMethod]
        public static string LoadUserControl(string controlName, int limit, int offset, int parentId)
        {
            return RenderControl(controlName, limit, offset, parentId);
        }

        public static string RenderControl(string controlName, int limit, int offset,int parentId)
        {
            Page page = new Page();
            ProfileList userControl = (ProfileList)page.LoadControl("~/UserControls/"+ controlName);
            userControl.EnableViewState = false;
            userControl.ID = "ux";

            int p;
            userControl.DataSource =GetProfiles(limit, offset,parentId,out p);
           
            userControl.BindControl();
            HtmlForm form = new HtmlForm();
            form.Controls.Add(userControl);
            page.Controls.Add(form);

            StringWriter textWriter = new StringWriter();
            HttpContext.Current.Server.Execute(page, textWriter, false);
            return textWriter.ToString();
        }

        private  static List<Profile> GetProfiles( int limit, int offset, int parentId, out int page)
        {
            List<Profile> profileList = new List<Profile>();         
            profileList = (new ProfileBLL()).GetLatestProfiles(parentId, limit, offset, out page);
           
            return profileList;
        }
    }
   

}
